// +----------------------------------------------------------------------
// | BossHongshi [鸿时互娱管理系统]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.hongshihuyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wgh
// +----------------------------------------------------------------------

module.exports = class extends think.cmswing.admin {
  constructor(ctx) {
    super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
    // 其他额外的操作
    this.tactive = 'finance';
  }
  /**
   * index action
   * @return {Promise} []
   */
  indexAction() {
    // auto render template file index_index.html
    return this.display();
  }

  /**
   * 财务日志
   */
  async logAction() {
    const list = await this.model('balance_log').order('time DESC').page(this.get('page') || 1, 20).countSelect();
    // console.log(list);
    const html = this.pagination(list);
    this.assign('pagerData', html);
    for (const itme of list.data) {
      itme.user_id = await this.model('cmswing/member').get_nickname(itme.user_id);
      itme.admin_id = await get_nickname(itme.admin_id);
    }
    this.assign('list', list.data);
    this.meta_title = '财务日志';
    return this.display();
  }
  
  /**
   * 提现申请
   */
  async withdrawAction() {
	  const status = this.get('status');
	  const map = {};
	  if (!think.isEmpty(status)) {
	      map.status = status;
	      this.assign('status', status);
	  }
	  const q = this.get('q');
	  if (!think.isEmpty(q)) {
	     map['withdraw_no|user_id'] = ['like', q];
	  }
    const list = await this.model('withdraw').where(map).order('id DESC').page(this.get('page') || 1, 20).countSelect();
    
    // console.log(list);
    const html = this.pagination(list);
    this.assign('pagerData', html);
//    for (const itme of list.data) {
//      itme.user_id = await this.model('cmswing/member').get_nickname(itme.user_id);
//      itme.admin_id = await get_nickname(itme.admin_id);
//    }
    for (const val of list.data) {
        switch (val.payment) {
          case 100:
            val.channel = '预付款支付';
            break;
          case 1001:
            val.channel = '货到付款';
            break;
          case 1002:
            val.channel = '银行汇款';
            break;
          default:
            val.channel = await this.model('pingxx').where({id: val.payment}).getField('title', true);
        }
      }
    this.assign('list', list.data);
    this.meta_title = '提现申请';
    return this.display();
  }
  
  async down2excelAction() {
    
      if (this.isAjax('get')) {
    	  const status = this.get('status');
    	  const map = {};
    	  if (!think.isEmpty(status)) {
    	      map.status = status;
    	  }
    	  const q = this.get('q');
    	  if (!think.isEmpty(q)) {
    	     map['withdraw_no|user_id'] = ['like', q];
    	  }
    	  //field('a.id,a.user_id,a.proxy_id,a.type,a.amount,a.amount_log,a.note,a.status,a.settle_time,a.create_time as time,a.origin_id').countSelect();
          const list = await this.model('withdraw').where(map).field('withdraw_no,create_time,amount,user_id,realname,bankno,bankname,alipayno,wxno,payment,status').order('id DESC').select();
          for (const val of list) {
        	  //val.amount = (val.amount, 1);
        	  val.create_time = dateformat('Y-m-d H:i',val.create_time);
        	  val.user_id = await this.model('cmswing/member').get_nickname(val.user_id);
              val.payment = await this.model('pingxx').where({id:val.payment}).getField('title', true);
              //item.status ==0
              if(val.status==0){
            	  val.status ="待审核";
              }else if(val.status==1){
            	  val.status ="打款中";
              }else if(val.status==2){
            	  val.status ="提现成功";
              }
          }
          console.log('提现记录--:'+JSON.stringify(list));
          if (!think.isEmpty(list)) {
            return this.success(list);
          } else {
            return this.fail('没有符合条件的记录');
          }
        } else {
          this.meta_title = '导出Excel';
          return this.display();
        }
}
  
  
};
